package com.project.baowei.dialog;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.project.baowei.adapter.SelectAdapter;
import com.project.baowei.bean.respoes.SelectBean;
import com.wangshen.base.dialog.base.BaseDialog;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class SelectDialog extends BaseDialog {
    private Button btn_area_choose;
    private RecyclerView recyclerView;
    private List<SelectBean> selectBeans;
    OnSelectClick onSelectClick;
    SelectBean bean;
    public OnSelectClick getOnSelectClick() {
        return onSelectClick;
    }

    public void setOnSelectClick(OnSelectClick onSelectClick) {
        this.onSelectClick = onSelectClick;
    }

    public SelectDialog(Context context) {
        super(context);
    }

    @Override
    public void initView() {
        setContentView(R.layout.dialog_select);
        btn_area_choose=findViewById(R.id.btn_area_choose);
        recyclerView=findViewById(R.id.rv_area_list);
    }

    public void show(List<SelectBean> list,String id) {
        super.show();
        this.selectBeans=list;
        for (SelectBean selectBean:selectBeans){
            if (selectBean.getId().equals(id)){
                selectBean.setIsSelect(true);
                bean=selectBean;
            }
        }
        SelectAdapter selectAdapter=new SelectAdapter(R.layout.item_select,selectBeans);
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter<?, ?> adapter, View view, int position) {
                for (SelectBean selectBean:selectBeans){
                    selectBean.setIsSelect(false);
                }
                selectBeans.get(position).setIsSelect(true);
                bean=selectBeans.get(position);
                selectAdapter.notifyDataSetChanged();
            }
        });
        btn_area_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onSelectClick != null){
                    onSelectClick.onSelect(bean);
                }
                dismiss();
            }
        });
    }


    @Override
    public void initData() {

    }

    @Override
    public int getView() {
        return R.layout.dialog_select;
    }
    public  static interface OnSelectClick{
        void onSelect(SelectBean selectBean);
    }
}
